import 'package:flutter/material.dart';
import 'package:power_house/pages/home/tab_search/filter_bar/data.dart';
import 'package:power_house/scoped_model/room_filter.dart';
import 'package:power_house/util/scoped_model_helper.dart';
import 'package:power_house/widgets/common_title.dart';

class FilterDrawer extends StatelessWidget {
  const FilterDrawer({super.key});

  @override
  Widget build(BuildContext context) {
    var dataList = ScopedModelHelper.getModel<FilterBarModel>(context).dataList;
    if (dataList['roomTypeList'] != null) {
      roomTypeList = dataList['roomTypeList']!;
    }
    if (dataList['orientedList'] != null) {
      orientedList = dataList['orientedList']!;
    }
    if (dataList['floorList'] != null) {
      floorList = dataList['floorList']!;
    }
    var selectIds = ScopedModelHelper.getModel<FilterBarModel>(context).selectedList.toList();

    void onChange(String data) {
      ScopedModelHelper.getModel<FilterBarModel>(context).selectedListChanged(data);
    }

    return Drawer(
      child: SafeArea(
        child: ListView(
          children: [
            const CommonTitle('户型'),
            FilterDrawerItem(
              list: roomTypeList,
              selectIds: selectIds,
              onChange: onChange,
            ),
            const CommonTitle('朝向'),
            FilterDrawerItem(
              list: orientedList,
              selectIds: selectIds,
              onChange: onChange,
            ),
            const CommonTitle('楼层'),
            FilterDrawerItem(
              list: floorList,
              selectIds: selectIds,
              onChange: onChange,
            ),
          ],
        ),
      ),
    );
  }
}

class FilterDrawerItem extends StatelessWidget {
  final List<GeneralType> list;
  final List<String> selectIds;
  final ValueChanged<String>? onChange;

  const FilterDrawerItem({
    super.key,
    required this.list,
    required this.selectIds,
    this.onChange,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 10.0, right: 10.0),
      child: Wrap(
        spacing: 10.0,
        runSpacing: 10.0,
        children: list.map((item) {
          var isActive = selectIds.contains(item.id);
          return GestureDetector(
            onTap: () {
              if (onChange != null) {
                onChange!(item.id);
              }
            },
            behavior: HitTestBehavior.translucent,
            child: Container(
              width: 100.0,
              height: 40.0,
              alignment: Alignment.center,
              decoration: BoxDecoration(color: isActive ? Colors.green : Colors.white, border: Border.all(width: 1.0, color: Colors.green)),
              child: Text(
                item.name,
                style: TextStyle(color: isActive ? Colors.white : Colors.grey),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }
}
